<!-- 玩家账号 -->
<template>
  <SearchTable :query="data.query" :list="data.list" :total="data.total" @className="rowClassName"
    @changeSize="changeSize" @changePage="changePage" @search="getSearchList"
    :showPagination="data.controlUserList.length ? true : false" @reset="resetDetails" @select="playerControl"
    @changeTh="data.dialogVisible = true">
    <template #other>
      <el-tabs v-model="data.query.type" type="card" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane :label="t('766')" :name="1" />
        <!-- <el-tab-pane label="历史控制记录" :name="2" /> -->
      </el-tabs>
    </template>
    <template #form>
      <el-form-item label="">
        <el-select class="selectWidth120" v-model="data.query.search_type" :placeholder="t('340')">
          <el-option v-for="item in data.conditionList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-input class="width200" v-model="data.query.search_value" :placeholder="t('19')" clearable
          onKeyUp="value=value.replace(/[\W]/g,'')" />
      </el-form-item>
      <el-form-item :label="t('767') + ':'">
        <el-date-picker v-model="data.query.date" type="daterange" range-separator="-" :start-placeholder="t('26')"
          :end-placeholder="t('27')" value-format="YYYY-MM-DD" clearable :disabled-date="indexStore.disabledDateTime"/>
      </el-form-item>
      <el-form-item :label="t('768') + ':'">
        <el-select v-model="data.query.game_id" :placeholder="t('340')" filterable>
          <el-option :label="t('21')" :value="-1" />
          <el-option v-for="(item, index) in indexStore.gameArray" :key="index"
            :label="'【' + item.value + '】' + GameName(indexStore.gameArray, item.value)" :value="item.value"
            v-show="item.type == 1 ? true : false" />
        </el-select>
      </el-form-item>
    </template>
    <template #button>
      <el-button type="danger" :disabled="data.controlUserList.length ? false : true"
        @click="data.isControlShow = true">{{ t('775') }}</el-button>
    </template>
    <template #table>
      <el-table-column type="selection" width="45" v-if="data.query.type == 1 ? true : false" />
      <template v-for="(item, index) in data.fields" :key="index">
        <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width"
          show-overflow-tooltip>
          <template #header>
            <el-tooltip v-if="item.tips" effect="dark" placement="top">
              <template #content>
                {{ item.tips }}
              </template>
              <el-icon class="ques-tips">
                <QuestionFilled />
              </el-icon>
            </el-tooltip>
            {{ item.label }}
          </template>
          <template v-if="item.prop == 'account3d_id'" #default="scope">
            <span class="item_check" @click="checkUser(scope.row.account3d_id, 1)">
              {{ scope.row.account3d_id }}
            </span>
          </template>
          <template v-if="item.prop == 'account3d'" #default="scope">
            <span class="item_details" @click="checkUser(scope.row.account3d, 3), data.checkedItem = scope.row">
              {{ scope.row.account3d }}
            </span>
          </template>
          <template v-if="item.prop == 'game_id'" #default="scope">
            <GameHeaderInfo :game_id="scope.row.game_id" class="flexCenter"/>
          </template>
          <template v-if="item.prop == 'rtp'" #default="scope">
            {{ BigHundred(scope.row.rtp) }}%
          </template>
          <template v-if="item.prop == 'game_rtp'" #default="scope">
            {{ BigHundred(scope.row.game_rtp) }}%
          </template>
          <template v-if="item.prop == 'history_rtp'" #default="scope">
            {{ BigHundred(scope.row.history_rtp) }}%
          </template>
          <template v-if="item.prop == 'control_that_rtp'" #default="scope">
            {{ scope.row.control_that_rtp ? BigHundred(scope.row.control_that_rtp) + '%' : '-' }}
          </template>
          <template v-if="item.prop == 'auto_cancel_rtp'" #default="scope">
            {{ scope.row.auto_cancel_rtp ? BigHundred(scope.row.auto_cancel_rtp) + '%' : '-' }}
          </template>
          <template v-if="item.prop == 'create_at'" #default="scope">
            {{ setTimestampTime(scope.row.create_at) }}
          </template>
        </el-table-column>
      </template>
      <el-table-column fixed="right" :label="t('97')" width="110" v-if="data.query.type == 1 ? true : false">
        <template #default="scope">
          <el-button type="danger" size="small" @click="delUserRtp(scope.row)">
            {{ t('763') }}
          </el-button>
        </template>
      </el-table-column>
    </template>
  </SearchTable>


  <!-- 变更表头弹框 -->
  <TableHeader :dialogVisible="data.dialogVisible" :list="data.fields" @cancel="data.dialogVisible = false"
    @resetData="resetData" />
  <!-- 玩家信息详情 -->
  <UserDetails :show="data.isUserDetails" @onHideDetails="onHideDetails" :checkedItem="data.checkedItem" />

  <!-- 批量调控 -->
  <el-dialog :modelValue="data.isControlShow" title="批量调控" width="45%" top="25vh" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="data.isControlShow = false">
    <template #header>
      <p class="box-title marginB5">{{ t('765') }}</p>
    </template>
    <div class="flexWrap">
      <span class="margin5" v-for="(item, index) in data.showUserList" :key="index">
        <div class="gameIcon flexCenter">
          【<span class="colorRed"> {{ item.account3d_id }}</span>】{{ item.account3d }}
        </div>
      </span>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="data.isControlShow = false">{{ t('240') }}</el-button>
        <el-button type="primary" @click="delAllControl">{{ t('241') }}</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, onMounted, ref } from 'vue'
import type { TabsPaneContext } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import TableHeader from '@/components/tableHeader/tableHeader.vue'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import UserDetails from '@/views/users/components/user-details.vue'
import GameHeaderInfo from '@/components/FormItem/GameHeaderInfo.vue'
import { GAMEICON } from '@/config/eunm'
import { selectToLabel, GameName, GamePlatform, setObjectArray } from '@/utils/index'
import { setTimestampTime, setTimeValue, getSevenWeek } from '@/utils/time'
import { BigHundred } from '@/utils/math'
import { getControlUser, changeAllControlUser, changeControlUser } from '@/api/user'

import { useStore } from '@/stores/index'
const indexStore = useStore()
import { useI18n } from 'vue-i18n'
const { t } = useI18n()

const data = reactive({
  query: {
    page: 1,
    size: 50,
    type: 1,
    search_type: 1,
    search_value: '',
    game_id: -1,
    date: getSevenWeek()
  },
  conditionList: [
    { label: t('17'), value: 1 },
    { label: t('18'), value: 3 }
  ],
  list: [],
  total: 0,
  fields: [
    { label: t('767'), prop: 'create_at', width: '170', visible: true, tips: t('771') },
    { label: t('17'), prop: 'account3d_id', width: '120', visible: true, tips: t('289') },
    { label: t('18'), prop: 'account3d', width: '180', visible: true, tips: t('290') },
    { label: t('41'), prop: 'game_id', width: '200', visible: true, tips: t('594') },
    { label: t('868'), prop: 'game_rtp', width: '150', visible: true, tips: t('871') },
    { label: t('869'), prop: 'history_rtp', width: '180', visible: true, tips: t('872') },
    { label: t('770'), prop: 'rtp', width: '120', visible: true, tips: t('618') },
    { label: t('874'), prop: 'control_that_rtp', width: '180', visible: true, tips: t('618') },
    { label: t('870'), prop: 'auto_cancel_rtp', width: '180', visible: true, tips: t('873') },
  ],
  dialogVisible: false,
  isUserDetails: false,
  isControlShow: false,
  checkedItem: {} as any,
  controlUserList: [] as any[],
  showUserList: [] as any[],
})

onMounted(async () => {
  getSearchList()
})

// 选择游戏tab
const handleClick = async (tab: TabsPaneContext) => {
  data.query.type = tab.paneName as number
  getSearchList()
}

const getSearchList = async () => {
  onHideDetails(false)
  data.query = setTimeValue(data.query)
  indexStore.setPageLoading(true)
  let res = await getControlUser(data.query)
  indexStore.setPageLoading(false)
  res.data.list?.forEach((item: any) => {
    item.uid = item.user_id
  })
  data.list = res.data.list
  data.total = res.data.total
}

// 选择玩家
const playerControl = (val: any) => {
  let arr = JSON.parse(JSON.stringify(val))
  data.controlUserList = val
  data.showUserList = setObjectArray(arr, 'account3d_id')
}
// 取消玩家RTP调控
const delUserRtp = async (row: any) => {
  let query = {} as any, message = '';
  query.agent_id = row.agent_id
  query.user_ids = [row.account3d_id]
  query.game_ids = [row.game_id]
  query.switch = 1
  query.rtp = 0
  message = `【${row.account3d_id}】${row.account3d}${t('773')}【${row.game_id}】${GameName(indexStore.gameArray, row.game_id)}`
  ElMessageBox.confirm(t('241') + t('240') + '( ' + message + ' )' + t('763'), t('650'), {
    confirmButtonText: t('241'),
    cancelButtonText: t('240')
  }).then(async () => {
    await changeControlUser(query)
    ElMessage.success(t('772'))
    onHideDetails(true)
  }).catch(() => { })
}
// 成功取消RTP调控调控
const delAllControl = async () => {
  let query = {} as any, message = '';
  query.user_ids = []
  query.game_ids = [0]
  data.controlUserList.forEach((item: any) => {
    query.user_ids.push(item.agent_id + '_' + item.account3d_id + '_' + item.game_id)
  })
  query.switch = 1
  query.rtp = 0
  if (!query.user_ids.length) {
    return ElMessage.error(t('774'))
  }
  await changeAllControlUser(query)
  ElMessage.success(t('772'))
  onHideDetails(true)
}

// 查看玩家信息
const checkUser = async (value: any, type: number) => {
  if (type == 1) {
    data.query.search_type = type
    data.query.search_value = value
    getSearchList()
  } else {
    data.isUserDetails = true
  }
}

// 玩家调控高亮
const rowClassName = (row: any) => {
  if (row.user_rtp_game_info?.length) {
    return 'warning-row'
  } else {
    return 'success-row'
  }
}

// 修改页数
const changeSize = (val: number) => {
  data.query.size = val
}

const changePage = (val: number) => {
  data.query.page = val
  getSearchList()
}

// 重置表头
const resetData = (val: any) => {
  data.fields = val
}

// 重置
const onHideDetails = (show: boolean) => {
  data.dialogVisible = false
  data.isUserDetails = false
  data.isControlShow = false
  data.checkedItem = {}
  data.controlUserList = []
  data.showUserList = []
  if (show) {
    getSearchList()
  }
}
const resetDetails = () => {
  data.query = {
    page: 1,
    size: 50,
    type: 1,
    search_type: 1,
    search_value: '',
    game_id: -1,
    date: getSevenWeek()
  }
  getSearchList()
}
</script>

<style scoped lang="scss"></style>
